package com.xxx.video.dynamic.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.adapters.BaseViewHolder;
import com.xxx.video.commen.configs.UserManager;
import com.xxx.video.commen.utils.ResourcesUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.main.beans.DynamicInfoBean;

import butterknife.BindView;

public class DynamicUserInfoAdapter extends BaseRecycleViewAdapter<Object, DynamicUserInfoAdapter.MyHolder> {

    private DynamicInfoBean dynamicInfoBean;

    public DynamicUserInfoAdapter(@NonNull Context context) {
        super(context);
    }

    @Override
    public MyHolder onCreateDefaultViewHolder(View view, int viewType) {
        return new MyHolder(view);
    }

    @Override
    public int getItemLayout(int viewType) {
        return R.layout.item_dynamic_details_person_info;
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public void setPersonData(DynamicInfoBean dynamicInfoBean) {
        this.dynamicInfoBean = dynamicInfoBean;
        notifyDataSetChanged();

    }

    @Override
    public boolean isSetBaseItemViewClickListener() {
        return false;
    }

    public void addFriendSuccess() {
        dynamicInfoBean.friendStatus = 1;
        notifyDataSetChanged();
    }

    public void collectionSuccess(boolean isCollection) {
        dynamicInfoBean.status = isCollection;
        notifyDataSetChanged();
    }

    public void unlikePlus() {
        if (dynamicInfoBean != null) {
            dynamicInfoBean.count = 2;
            dynamicInfoBean.dynamic.fUnlikeCount++;
            notifyDataSetChanged();
        }
    }

    public void starPlus() {
        if (dynamicInfoBean != null) {
            dynamicInfoBean.count = 1;
            dynamicInfoBean.dynamic.fLikeCount++;
            notifyDataSetChanged();
        }
    }

    class MyHolder extends BaseViewHolder {


        @BindView(R.id.iv_item_dynamic_details_person_info_avatar)
        ImageView ivItemDynamicDetailsPersonInfoAvatar;
        @BindView(R.id.tv_item_dynamic_details_person_info_name)
        TextView tvItemDynamicDetailsPersonInfoName;
        @BindView(R.id.tv_item_dynamic_details_person_info_chat)
        TextView tvItemDynamicDetailsPersonInfoChat;
        @BindView(R.id.tv_item_dynamic_details_fragment_details_like_count)
        TextView tvItemDynamicDetailsFragmentDetailsLikeCount;
        @BindView(R.id.tv_item_dynamic_details_fragment_details_un_like_count)
        TextView tvItemDynamicDetailsFragmentDetailsUnLikeCount;
        @BindView(R.id.tv_item_dynamic_details_fragment_details_favorites)
        TextView tvItemDynamicDetailsFragmentDetailsFavorites;

        public MyHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData2View(int position) {
            DynamicInfoBean.DynamicBean dynamicBean = dynamicInfoBean.dynamic;
            /*
             * avatar
             */
            Glide.with(context).load(ResourcesUtils.getInstance().getImagePath(dynamicBean.fUsePicurl))
                    .apply(new RequestOptions().circleCrop()
                            .placeholder(R.drawable.icon_default_avatar)
                            .error(R.drawable.icon_default_avatar)
                    ).into(ivItemDynamicDetailsPersonInfoAvatar);
            if (dynamicInfoBean.friendStatus == 0) {
                tvItemDynamicDetailsPersonInfoChat.setTextColor(Color.WHITE);
                tvItemDynamicDetailsPersonInfoChat.setText(StringUtils.getInstance().getString(R.string.tv_item_dynamic_details_person_info_add_friend));
                tvItemDynamicDetailsPersonInfoChat.setBackgroundResource(R.drawable.bg_item_people_nearby_horizontal_list_add);
            } else if (dynamicInfoBean.friendStatus == 1) {
                tvItemDynamicDetailsPersonInfoChat.setTextColor(ResourcesUtils.getInstance().getColor(R.color.c_333333));
                tvItemDynamicDetailsPersonInfoChat.setText(StringUtils.getInstance().getString(R.string.item_friend_request_ing));
                tvItemDynamicDetailsPersonInfoChat.setBackground(null);
            } else if (dynamicInfoBean.friendStatus == 2) {
                tvItemDynamicDetailsPersonInfoChat.setTextColor(Color.WHITE);
                tvItemDynamicDetailsPersonInfoChat.setBackgroundResource(R.drawable.bg_item_people_nearby_horizontal_list_add);
                tvItemDynamicDetailsPersonInfoChat.setText(StringUtils.getInstance().getString(R.string.tv_item_dynamic_details_person_info_chat));
            }
            tvItemDynamicDetailsPersonInfoChat.setVisibility(View.VISIBLE);
            if (dynamicInfoBean.dynamic.fUserName.equals(UserManager.getInstance().getUserName())) {
                tvItemDynamicDetailsPersonInfoChat.setVisibility(View.GONE);
            }
            tvItemDynamicDetailsFragmentDetailsFavorites.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_item_video_details_fragment_details_favorites, 0, 0);

            if (dynamicInfoBean.status) {
                tvItemDynamicDetailsFragmentDetailsFavorites.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_item_video_details_fragment_details_favorites_press, 0, 0);
            }
            /*
             * like count
             */
            tvItemDynamicDetailsFragmentDetailsLikeCount.setText(StringUtils.getInstance().getNotEmptyMsg(String.valueOf(dynamicBean.fLikeCount)));
            /*
             * un like count
             */
            tvItemDynamicDetailsFragmentDetailsUnLikeCount.setText(StringUtils.getInstance().getNotEmptyMsg(String.valueOf(dynamicBean.fUnlikeCount)));
            tvItemDynamicDetailsFragmentDetailsLikeCount.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_item_video_details_fragment_details_like_count, 0, 0);
            tvItemDynamicDetailsFragmentDetailsUnLikeCount.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_item_video_details_fragment_details_un_like_count, 0, 0);
            if (dynamicInfoBean.count == 1) {
                tvItemDynamicDetailsFragmentDetailsLikeCount.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_item_video_details_fragment_details_like_count_check, 0, 0);
            }
            if (dynamicInfoBean.count == 2) {
                tvItemDynamicDetailsFragmentDetailsUnLikeCount.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_item_video_details_fragment_details_un_like_count_check, 0, 0);
            }
            /*
             * name
             */
            tvItemDynamicDetailsPersonInfoName.setText(StringUtils.getInstance().getNotEmptyMsg(dynamicBean.fUserName));
            tvItemDynamicDetailsPersonInfoChat.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(null, 0, dynamicInfoBean.friendStatus);
                }
            });

            tvItemDynamicDetailsFragmentDetailsLikeCount.setOnClickListener(v -> {
                if (dynamicInfoBean.count == 1) {
                    return;
                }
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(dynamicBean, 0, 3);
                }
            });
            tvItemDynamicDetailsFragmentDetailsFavorites.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(dynamicBean, 0, 4);
                }
            });
            tvItemDynamicDetailsFragmentDetailsUnLikeCount.setOnClickListener(v -> {
                if (dynamicInfoBean.count == 2) {
                    return;
                }
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(dynamicBean, 0, 5);
                }
            });
        }
    }
}
